// SHYUXINPC SEO 优化脚本 add_action('wp_footer', 'shyuxinpc_seo_scripts'); function shyuxinpc_seo_scripts() { if (is_front_page() || is_home() || is_page(14)) { echo ""; echo ""; } } // shyuxinpc_seo_fix add_action('wp_footer', 'shyuxinpc_seo_fix_js'); function shyuxinpc_seo_fix_js() { echo ''; } // === SHYUXINPC Schema Markup === add_action('wp_head', 'shyuxinpc_schema_markup', 20); function shyuxinpc_schema_markup() { // LocalBusiness Schema on front page if (is_front_page() || is_home() || is_page(14)) { echo '' . PHP_EOL; } // Breadcrumb Schema on all pages $crumbs = array( array("name" => "Home", "url" => home_url("/")) ); if (is_single() || is_page()) { $crumbs[] = array("name" => get_the_title(), "url" => get_permalink()); } elseif (is_category() || is_product_category()) { $cat = get_queried_object(); if ($cat) { $crumbs[] = array("name" => "Products", "url" => home_url("/shop/")); $crumbs[] = array("name" => $cat->name, "url" => get_term_link($cat)); } } $items = array(); $i = 1; foreach ($crumbs as $crumb) { $items[] = array( "@type" => "ListItem", "position" => $i, "name" => $crumb["name"], "item" => $crumb["url"] ); $i++; } $breadcrumb_json = json_encode(array( "@context" => "https://schema.org", "@type" => "BreadcrumbList", "itemListElement" => $items ), JSON_UNESCAPED_SLASHES); echo '' . PHP_EOL; } // === END Schema Markup ====== // === Product Schema for WooCommerce (Safe Version) === add_action('wp_head', 'shyuxinpc_product_schema'); function shyuxinpc_product_schema() { // 只在 WooCommerce 产品页面执行 if (!function_exists('is_product') || !is_product()) return; global $product; if (!isset($product) || !is_object($product)) return; try { $name = method_exists($product, 'get_name') ? $product->get_name() : get_the_title(); $price = method_exists($product, 'get_price') ? $product->get_price() : ''; $in_stock = method_exists($product, 'is_in_stock') ? $product->is_in_stock() : true; $desc = ''; if (method_exists($product, 'get_short_description')) { $desc = wp_strip_all_tags($product->get_short_description()); } if (empty($desc) && method_exists($product, 'get_description')) { $desc = wp_strip_all_tags($product->get_description()); } $schema = array( "@context" => "https://schema.org", "@type" => "Product", "name" => $name, "description" => $desc ); if (!empty($price)) { $schema["offers"] = array( "@type" => "Offer", "priceCurrency" => "USD", "price" => $price, "availability" => $in_stock ? "https://schema.org/InStock" : "https://schema.org/OutOfStock" ); } echo '' . PHP_EOL; } catch (Exception $e) { // 静默失败,不影响页面渲染 } } // === END Product Schema === // === Breadcrumbs (PHP Generated, No AIOSEO Dependency) === add_action('wp_footer', 'shyuxinpc_breadcrumbs_final', 20); function shyuxinpc_breadcrumbs_final() { if (is_front_page() || is_home()) return; global $post; $crumbs = array(); $crumbs[] = array('name' => 'Home', 'url' => home_url('/')); if (is_singular('product') && function_exists('wc_get_product')) { $product = wc_get_product(get_the_ID()); if ($product) { $terms = get_the_terms(get_the_ID(), 'product_cat'); if ($terms && !is_wp_error($terms)) { $term = reset($terms); $crumbs[] = array('name' => 'Products', 'url' => get_permalink(wc_get_page_id('shop'))); $crumbs[] = array('name' => $term->name, 'url' => get_term_link($term)); } $crumbs[] = array('name' => get_the_title(), 'url' => ''); } } elseif (is_product_category()) { $cat = get_queried_object(); $crumbs[] = array('name' => 'Products', 'url' => get_permalink(wc_get_page_id('shop'))); $crumbs[] = array('name' => $cat->name, 'url' => ''); } elseif (is_page()) { $crumbs[] = array('name' => get_the_title(), 'url' => ''); } elseif (is_single()) { $categories = get_the_category(); if ($categories && !is_wp_error($categories)) { $cat = reset($categories); $crumbs[] = array('name' => $cat->name, 'url' => get_category_link($cat)); } $crumbs[] = array('name' => get_the_title(), 'url' => ''); } if (count($crumbs) < 2) return; // 构建面包屑 HTML $html = '
'; $html .= 'You are here: '; foreach ($crumbs as $i => $crumb) { if ($i > 0) $html .= ' » '; if (!empty($crumb['url'])) { $html .= '' . esc_html($crumb['name']) . ''; } else { $html .= '' . esc_html($crumb['name']) . ''; } } $html .= '
'; // 用 JS 插入到页面顶部 echo ''; } // === END Breadcrumbs Final ======= // === Footer Navigation Links for SEO === add_action('wp_footer', 'shyuxinpc_footer_nav', 5); function shyuxinpc_footer_nav() { if (is_front_page() || is_home()) return; echo '' . PHP_EOL; } // === END Footer Nav === // test save // test save